#include "mode_key_handle.h"
void mode_net_handle(SYS_OPT_KEY opt)
{  
    if(OPT_MATCH_NET==opt.oepation) //判断是否是配网模式
    {
        come_to_match_network();
    }
}

SYS_OPT_KEY on_off_opt(EM_DRV_ACT key_act)
{
    SYS_OPT_KEY opt={.oepation=DRV_ACT_NONE};
    if (key_act == DRV_ACT_KEY_1S)
    {
        opt.oepation = DRV_ACT_NONE;
    }
    else if((key_act == DRV_ACT_KEY_SHORT)&&(key_act != DRV_ACT_KEY_1S))
    {
        if(app_logic_get_ssp() == SSP_POWER_OFF) //判断当前状态
        {
            opt.oepation = OPT_POWER_TURNON; //开机
        }
        else
        {
            opt.oepation = OPT_POWER_TURNOFF; //关机
        }
    }
    LOG_INFO("on_off_opt %d\n",opt.oepation);
    return opt;
}

SYS_OPT_KEY rest_net_opt(EM_DRV_ACT key_act)
{
    SYS_OPT_KEY opt={.oepation=DRV_ACT_NONE};
    if(key_act == DRV_ACT_KEY_5S) //开始配网
    {
        opt.oepation = OPT_MATCH_NET;
        LOG_INFO("match network\n");
    }
    LOG_INFO("on_off_opt %d\n",opt.oepation);
    return opt;
}

SYS_OPT_KEY mode_opt(EM_DRV_ACT key_act)
{
    SYS_OPT_KEY opt;
    if (key_act == DRV_ACT_KEY_DOUBLE)
    {
        if(app_logic_get_ssp() != SSP_POWER_OFF && (app_logic_get_ssp() != SSP_LOGO))
        {
            opt.oepation = OPT_GO_NEXT; //下一步
        }
        else
        {
            opt.oepation = OPT_INVALID; //无效操作
        }
    }
    else
    {
        opt.oepation = OPT_INVALID;
    }
    LOG_INFO("mode_opt %d\n",opt.oepation);
    return opt;
}


SYS_OPT_KEY key_handle(EM_DRV_ACT key_act)
{
    SYS_OPT_KEY opt={.oepation=DRV_ACT_NONE};
    switch (key_act)
    {
    case DRV_ACT_KEY_1S: //长按 1S
        opt.oepation = DRV_ACT_NONE;
        break;
    case DRV_ACT_KEY_SHORT: //短按
        opt=on_off_opt(key_act);
        break;
    case DRV_ACT_KEY_5S: //长按 5s
        opt=rest_net_opt(key_act);
        break;
    case DRV_ACT_KEY_DOUBLE: //双击
        opt=mode_opt(key_act);
    default:
        break;
    }
    return opt;
}


SYS_OPT_KEY brightness_reduce_opt(EM_ENCODER_ACT key_act)
{
    SYS_OPT_KEY opt;

    if((app_logic_get_ssp() != SSP_POWER_OFF))
    {
        if(key_act == ENCODER_ACT_TL_L)
        {
            opt.oepation = OPT_GO_REDUCE; //减小
        }
        else if(key_act == ENCODER_ACT_TL_H)
        {
            opt.oepation = OPT_GO_REDUCE_CONTINUE; //快速减少
        }
        else
        {
            opt.oepation = OPT_INVALID;
        }
    }
    else
    {
        opt.oepation = OPT_INVALID; //无效操作
    }

    LOG_INFO("brightness_reduce_opt %d\n",opt.oepation);
    return opt;
}

SYS_OPT_KEY brightness_add_opt(EM_ENCODER_ACT key_act)
{
    SYS_OPT_KEY opt;
    if((app_logic_get_ssp() != SSP_POWER_OFF))
    {
        if (key_act == ENCODER_ACT_TR_L)
        {
            opt.oepation = OPT_GO_ADD;
        }
        else if(key_act == ENCODER_ACT_TR_H)
        {
            opt.oepation = OPT_GO_ADD_CONTINUE; 
        }
        else
        {
            opt.oepation = OPT_INVALID;
        }
    }
    else
    {
        opt.oepation = OPT_INVALID; //无效操作
    }
    return opt;
}

SYS_OPT_KEY press_encode_TR_handle(EM_ENCODER_ACT key_act)
{
    SYS_OPT_KEY opt;
    if (key_act == ENCODER_PRE_TR)
    {
        opt.oepation = OPT_PREE_TR;
    }
    return opt;
}

SYS_OPT_KEY press_encode_TL_handle(EM_ENCODER_ACT key_act)
{
    SYS_OPT_KEY opt;
    if (key_act == ENCODER_PRE_TR)
    {
       opt.oepation = OPT_PREE_TL;
    }
    return opt;
}


SYS_OPT_KEY encode_handle(EM_ENCODER_ACT key_act) //编码器源处理
{
    SYS_OPT_KEY opt={.oepation=DRV_ACT_NONE};
    switch(key_act)
    {
        case ENCODER_ACT_TL_L: //低速左转
            opt=brightness_reduce_opt(key_act);
            break;

        case ENCODER_ACT_TL_H: //高速左转
            opt=brightness_reduce_opt(key_act);
            break;

        case ENCODER_ACT_TR_L: //低速右转处理
            opt=brightness_add_opt(key_act);
            break;

        case ENCODER_ACT_TR_H: ///高速右转处理
            opt=brightness_add_opt(key_act);
            break;

        case ENCODER_PRE_TR://按压右转
            opt=press_encode_TR_handle(key_act);
            break;

        case ENCODER_PRE_TL://按压左转
            opt=press_encode_TL_handle(key_act);
            break;
        default:
            break;
    }
    return opt;
}


SYS_OPT_KEY app_logic_act_to_opt(key_act_param key_act)
{
    SYS_OPT_KEY opt;
    if(key_act.drvAct == DRV_ACT_NONE)
    {
      opt.oepation = OPT_NONE;
    }

    switch (key_act.act_source)
    {
      case ACT_KEY: //按键源处理
        opt = key_handle(key_act.drvAct);
        break;
        
      case ACT_ENCODE: //编码器源
        opt = encode_handle(key_act.drvAct);
        break;

      default:
        break;
    }
    LOG_INFO("opt.oepation is %d\n",opt.oepation);
    return opt;
}
